<?php
/**
 * @version $Id: unocart.searchbot.php 22 2010-05-17 22:03:42Z vladzimirs@gmail.com $
 * @package UnoCart
 * @copyright Авторские права (C) 2009-2010 UnoCart team & (C) 2007-2010 Joostina team &. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * UnoCart! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл help/copyright.php.
 */

/** ensure this file is being included by a parent file */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

/** Register search function inside Mambo's API */
$_MAMBOTS->registerFunction('onSearch', 'botSearchUnocart');

/**
 * Search method
 *
 * The sql must return the following fields that are used in a common display
 * routine: href, title, section, created, text, browsernav
 */
function botSearchUnocart($text, $phrase = '', $ordering = '')
{
    global $database;
    $text = Jstring::trim($text);
    if ($text == '') {
        return array();
    }
    $wheres = array();
    switch ($phrase) {
        case 'exact':
            $wheres2 = array();
            $wheres2[] = "LOWER(prod_descr.product_name) LIKE '%$text%'";
            $wheres2[] = "LOWER(prod_descr.product_description) LIKE '%$text%'";
            $wheres2[] = "LOWER(prod_descr.product_short_description) LIKE '%$text%'";
            $where = '(' . implode(') OR (', $wheres2) . ')';
            break;
        case 'all':
        case 'any':
        default:
            $words = explode(' ', $text);
            $wheres = array();
            foreach ($words as $word) {
                $wheres2 = array();
                $wheres2[] = "LOWER(prod_descr.product_name) LIKE '%$word%'";
                $wheres2[] = "LOWER(prod_descr.product_description) LIKE '%$word%'";
                $wheres2[] = "LOWER(prod_descr.product_short_description) LIKE '%$word%'";
                $wheres[] = implode(' OR ', $wheres2);
            }
            $where = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
            break;
    }

    switch ($ordering) {
        case 'newest':
        default:
            $order = 'prod.product_date_added DESC';
            break;
        case 'oldest':
            $order = 'prod.product_date_added ASC';
            break;
        case 'popular':
            $order = 'prod_descr.product_name ASC';
            break;
        case 'alpha':
            $order = 'prod_descr.product_name ASC';
            break;
        case 'category':
            $order = 'categ.category_id ASC';
            break;
    }


    $database->setQuery("SELECT menu.id, menu.name FROM  `#__menu` AS menu WHERE menu.link LIKE '%com_unocart%' AND menu.published=1 AND menu.access=0");
    $database->loadObject($Item);
    $ItemName = !empty($Item->name) ? $Item->name : "shop";
    $Itemid = !empty($Item->id) ? $Item->id : "1";

    $query = "SELECT DISTINCT prod.product_id, prod_descr.product_name as title," .
        "\n    prod.product_date_added AS created," . "\n    prod_descr.product_short_description AS text," .
        "\n    CONCAT('$ItemName/',categ_descr.category_name) as section," . "\n    CONCAT('index.php?option=com_unocart&task=product&product_id=',prod.product_id,'&category_id=',categ.category_id, '&Itemid=" .
        $Itemid . "' ) as href," . "\n    '2' as browsernav" . "\n FROM #__unocart_products AS prod" .
        "\n LEFT JOIN `#__unocart_products_description` AS prod_descr ON prod.product_id = prod_descr.product_id" .
        "\n LEFT JOIN `#__unocart_products_to_categories` AS prod_cat ON prod_cat.product_id = prod.product_id" .
        "\n LEFT JOIN `#__unocart_categories` AS categ ON prod_cat.category_id = categ.category_id" .
        "\n LEFT JOIN `#__unocart_categories_description` AS categ_descr ON categ.category_id = categ_descr.category_id" .
        "\n WHERE $where" . "\n AND prod.product_publish='1'" . "\n GROUP BY prod.product_id ORDER BY $order";
    $database->setQuery($query);

    $rows = $database->loadObjectList();
    echo $database->stderr();
    foreach ($rows as $key => $value) {
        $rows[$key]->href = sefRelToAbs($value->href);
    }
    return $rows;
}

?>
